﻿#pragma once
#include<assert.h>


template<class K,class V>
struct AVLTreeNode
{
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	pair<K, V> _kv;
	int _bf;//balance factor

	AVLTreeNode(const pair<K,V>& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_kv(kv)
		,_bf(0)
	{}
};


template<class K,class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:

	bool Insert(const pair<K, V>& kv)
	{
		/**********插入思路**************************************************************
		* 1. 先按照二叉搜索树的规则将节点插入到AVL树中
		* 2. 新节点插入后，AVL树的平衡性可能会遭到破坏，此时就需要更新平衡因子，并检测是否
		* 破坏了AVL树的平衡性，失去平衡的话，需要进行平衡调整
		********************************************************************************/

		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;
		while (cur != nullptr)//查找插入位置
		{
			if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		//确定链接关系
		cur = new Node(kv);
		if (parent->_kv.first>kv.first)
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		//检查是否平衡
		//……
		/*
		pCur插入后，pParent的平衡因子一定需要调整，在插入之前，pParent
		的平衡因子分为三种情况：-1，0, 1, 分以下两种情况：
 		1. 如果pCur插入到pParent的左侧，只需给pParent的平衡因子-1即可
 		2. 如果pCur插入到pParent的右侧，只需给pParent的平衡因子+1即可

		此时：pParent的平衡因子可能有三种情况：0，正负1， 正负2
		1. 如果pParent的平衡因子为0，说明插入之前pParent的平衡因子为正负1，插入后被调整
		成0，此时满足AVL树的性质，插入成功
		2. 如果pParent的平衡因子为正负1，说明插入前pParent的平衡因子一定为0，插入后被更
		新成正负1，此时以pParent为根的树的高度增加，需要继续向上更新
 		3. 如果pParent的平衡因子为正负2，则pParent的平衡因子违反平衡树的性质，需要对其进
		行旋转处理
		*/
		while (parent)
		{
			//更新平衡因子
			if (parent->_left == cur)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}
			//判断是否需要进行旋转
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				//继续向上调整平衡因子
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				//不平衡，进行旋转降低高度
				if (parent->_bf == 2&& cur->_bf == 1)//RR型
				{
					RotateL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == -1)//LL型
				{
					RotateR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)//RL型
				{
					RotateRL(parent);
				}
				else if (parent->_bf == -2 && cur->_bf == 1)//LR型
				{
					RotateLR(parent);
				}
				break;
			}
			else
			{
				assert(false);
			}
		}
		return true;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool IsBalance()
	{
		return _IsBalance(_root);
	}
	int Height()
	{
		return _Height(_root);
	}
private:
	int _Height(Node* root)
	{
		if (root == nullptr)return 0;
		int left = _Height(root->_left);
		int right = _Height(root->_right);
		return left > right ? left + 1 : right + 1;
	}
	bool _IsBalance(Node* root)
	{
		if (root==nullptr)return true;

		int left = _Height(root->_left);
		int right = _Height(root->_right);

		if (right - left != root->_bf)
		{
			cout << "结点平衡因子异常" << endl;
			return false;
		}

		return abs(left - right) < 2
			&& _IsBalance(root->_left)
			&& _IsBalance(root->_right);
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)return;
		_InOrder(root->_left);
		cout << root->_kv.first << " ";
		_InOrder(root->_right);

	}
	void RotateR(Node* parent)
	{
		assert(parent != nullptr);

		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		//更新链接关系

		parent->_left = subLR;
		subL->_right = parent;

		//维护_parent指针
		if (subLR != nullptr)
		{
			subLR->_parent = parent;
		}

		Node* ppnode = parent->_parent;
		parent->_parent = subL;

		if (ppnode == nullptr)
		{
			_root = subL;
			_root->_parent = nullptr;
		}
		else
		{
			//链接ppnode和subL之间的关系
			if (ppnode->_left == parent)
			{
				ppnode->_left = subL;
			}
			else
			{
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}

		//更新平衡因子
		subL->_bf = parent->_bf = 0;
	}
	void RotateL(Node* parent)
	{
		assert(parent != nullptr);

		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		//更新链接关系

		parent->_right = subRL;
		subR->_left = parent;

		//维护_parent指针
		if (subRL != nullptr)
		{
			subRL->_parent = parent;
		}

		Node* ppnode = parent->_parent;
		parent->_parent = subR;

		if (ppnode == nullptr)
		{
			_root = subR;
			_root->_parent = nullptr;
		}
		else
		{
			//链接ppnode和subL之间的关系
			if (ppnode->_left == parent)
			{
				ppnode->_left = subR;
			}
			else
			{
				ppnode->_right = subR;
			}
			subR->_parent = ppnode;
		}

		//更新平衡因子
		subR->_bf = parent->_bf = 0;
	}
	void RotateLR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;

		RotateL(subL);
		RotateR(parent);

		if (bf == 1)
		{
			subL->_bf = -1;
			parent->_bf = 0;
			subLR->_bf = 0;
		}
		else if (bf == -1)
		{
			subL->_bf = 0;
			parent->_bf = 1;
			subLR->_bf = 0;
		}
		else if (bf == 0)
		{
			subL->_bf = 0;
			parent->_bf = 0;
			subLR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
	void RotateRL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		int bf = subRL->_bf;

		RotateR(subR);
		RotateL(parent);

		if (bf == 1)
		{
			subR->_bf = 0;
			parent->_bf = -1;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			subR->_bf = 1;
			parent->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == 0)
		{
			subR->_bf = 0;
			parent->_bf = 0;
			subRL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}
private:
	Node* _root = nullptr;
};

void Test_AVLTree1()
{
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
	//int a[] = { 1,2,3,4,5,6,7,8,9,10 };
	//int a[] = { 10,9,8,7,6,5,4,3,2,1 };
	AVLTree<int, int> t1;
	for (auto e : a)
	{
		t1.Insert(make_pair(e, e));
	}

	cout << t1.IsBalance() << endl;
	t1.InOrder();
}

void Test_AVLTree2()
{
	srand((unsigned int)time(0));
	const size_t N = 5000000;
	AVLTree<int, int> t;
	for (size_t i = 0; i < N; ++i)
	{
		size_t x = rand() + i;
		t.Insert(make_pair(x, x));
		//cout << t.IsBalance() << endl;
	}

	//t.Inorder();

	cout << t.IsBalance() << endl;
	cout << t.Height() << endl;
}

